package swagger2;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import swagger2.domain.AuthAPI;
import swagger2.domain.PublicAPI;
import swagger2.handler.TokenAspect;

@Configuration
@EnableSwagger2
@ComponentScan(basePackages = {
	"swagger2"
})
public class Swagger2
{

	@Bean
	public Docket createPublicApi()
	{
		return new Docket(DocumentationType.SWAGGER_2)
			.groupName("公开接口")
			.apiInfo(publicApiInfo())
			.select()
			.apis(RequestHandlerSelectors.withClassAnnotation(PublicAPI.class))
			.paths(PathSelectors.any())
			.build();
	}

	@Bean
	public Docket createAuthApi()
	{
		return new Docket(DocumentationType.SWAGGER_2)
			.groupName("授权接口")
			.apiInfo(authApiInfo())
			.select()
			.apis(RequestHandlerSelectors.withClassAnnotation(AuthAPI.class))
			.paths(PathSelectors.any())
			.build()
			.globalOperationParameters(Lists.newArrayList(new ParameterBuilder()
				.name(TokenAspect.TOKEN_NAME)
				.description("令牌")
				.modelRef(new ModelRef("string"))
				.parameterType("header")
				.required(true)
				.build()));
	}

	private ApiInfo authApiInfo()
	{
		return new ApiInfoBuilder()
			.title("授权接口")
			.version("1.0")
			.contact(new Contact("alldios", "http://note.alldios.com", "alldios@outlook.com"))
			.description("更多Spring Boot相关文章请关注微信公众号alldios")
			.termsOfServiceUrl("http://note.alldios.com")
			.build();
	}

	private ApiInfo publicApiInfo()
	{
		return new ApiInfoBuilder()
			.title("公共接口")
			.version("1.0")
			.contact(new Contact("alldios", "http://note.alldios.com", "alldios@outlook.com"))
			.description("更多Spring Boot相关文章请关注微信公众号alldios")
			.termsOfServiceUrl("http://note.alldios.com")
			.build();
	}

}
